package com.ds.core.support.qiniu;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Paths;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.persistent.FileRecorder;

public class Uploader {

//	private static String accessKey = PropertiesUtil.getString("qiniu.AccessKey");
//	private static String secretKey = PropertiesUtil.getString("qiniu.SecretKey");
//	private static String bucket = PropertiesUtil.getString("qiniu.bucket");
	private static String bucket = "ds-literary";

	/**
	 * 文件上传
	 * @param zone 
	 *    华东	Zone.zone0()
	 *    华北	Zone.zone1()
	 *    华南	Zone.zone2()
	 *    北美	Zone.zoneNa0()
	 * @param upToken 上传凭证
	 * @param localFilePath 需要上传的文件本地路径
	 * @param key 若key为null，以文件内容的hash值作为文件名
	 * @return
	 */
	public static DefaultPutRet fileUpload(Zone zone,String upToken,String localFilePath,String key) {
		// 构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(zone);
		// ...其他参数参考类注释
		UploadManager uploadManager = new UploadManager(cfg);
		// 如果是Windows情况下，格式是 D:\\qiniu\\test.png
		//String localFilePath = "/home/qiniu/test.png";
		//String localFilePath = "D:\\qiniu\\test.jpg";
		try {
			Response response = uploadManager.put(localFilePath, key, upToken);
			// 解析上传成功的结果
			DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
			System.out.println(putRet.key);
			System.out.println(putRet.hash);
			return putRet;
		} catch (QiniuException ex) {
			Response r = ex.response;
			System.err.println(r.toString());
			try {
				System.err.println(r.bodyString());
			} catch (QiniuException ex2) {
				// ignore
			}
		}
		return null;
	}

	/**
	 * 字符组上传
	 * @param zone
	 * @param upToken
	 * @param key 若key为null，以文件内容的hash值作为文件名
	 * @return
	 */
	public static DefaultPutRet charArrayUpload(Zone zone,String upToken,String key) {
		// 构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(zone);
		// ...其他参数参考类注释
		UploadManager uploadManager = new UploadManager(cfg);
		// 默认不指定key的情况下，以文件内容的hash值作为文件名
		try {
			byte[] uploadBytes = "hello qiniu cloud".getBytes("utf-8");

			try {
				Response response = uploadManager.put(uploadBytes, key, upToken);
				// 解析上传成功的结果
				DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
				System.out.println(putRet.key);
				System.out.println(putRet.hash);
				return putRet;
			} catch (QiniuException ex) {
				Response r = ex.response;
				System.err.println(r.toString());
				try {
					System.err.println(r.bodyString());
				} catch (QiniuException ex2) {
					// ignore
				}
			}
		} catch (UnsupportedEncodingException ex) {
			// ignore
		}
		return null;
	}

	/**
	 * 数据流上传
	 * @param zone
	 * @param upToken
	 * @param key 若key为null，以文件内容的hash值作为文件名
	 * @return
	 */
	public static DefaultPutRet streamUpload(Zone zone,String upToken,String key) {
		// 构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(zone);
		// ...其他参数参考类注释
		UploadManager uploadManager = new UploadManager(cfg);
		// ...生成上传凭证，然后准备上传
		try {
			byte[] uploadBytes = "test streamUpload \n hello qiniu cloud".getBytes("utf-8");
			ByteArrayInputStream byteInputStream = new ByteArrayInputStream(uploadBytes);
			try {
				Response response = uploadManager.put(byteInputStream, key, upToken, null, null);
				// 解析上传成功的结果
				DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
				System.out.println(putRet.key);
				System.out.println(putRet.hash);
				return putRet;
			} catch (QiniuException ex) {
				Response r = ex.response;
				System.err.println(r.toString());
				try {
					System.err.println(r.bodyString());
				} catch (QiniuException ex2) {
					// ignore
				}
			}
		} catch (UnsupportedEncodingException ex) {
			// ignore
		}
		return null;
	}

	/**
	 * 断点续传
	 * @param zone
	 * @param upToken
	 * @param localFilePath
	 * @param key 若key为null，以文件内容的hash值作为文件名
	 * @return
	 */
	public static DefaultPutRet breakPointUpload(Zone zone,String upToken,String localFilePath,String key) {
		// 构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(zone);
		// ...其他参数参考类注释
		// 如果是Windows情况下，格式是 D:\\qiniu\\test.png
		String localTempDir = Paths.get(System.getenv("java.io.tmpdir"), bucket).toString();
		try {
			// 设置断点续传文件进度保存目录
			FileRecorder fileRecorder = new FileRecorder(localTempDir);
			UploadManager uploadManager = new UploadManager(cfg, fileRecorder);
			try {
				Response response = uploadManager.put(localFilePath, key, upToken);
				// 解析上传成功的结果
				DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
				System.out.println(putRet.key);
				System.out.println(putRet.hash);
				return putRet;
			} catch (QiniuException ex) {
				Response r = ex.response;
				System.err.println(r.toString());
				try {
					System.err.println(r.bodyString());
				} catch (QiniuException ex2) {
					// ignore
				}
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		// MyPutRet myPutRet=response.jsonToObject(MyPutRet.class);
		return null;
	}

	
	public static void main(String[] args) {
		//fileUpload(Zone.zone0(),CredentialsManager.getUploadCredential(),"D:\\qiniu\\test.jpg",null);
		//charArrayUpload(Zone.zone0(),CredentialsManager.getUploadCredential(),null);
		//streamUpload(Zone.zone0(),CredentialsManager.getUploadCredential(),null);
		breakPointUpload(Zone.zone0(),CredentialsManager.getUploadCredential(),"D:\\qiniu\\test.jpg",null);
	}
}
